import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'

import store from '../store'


Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login.vue')
    },
    {
        path: '/iotManage',
        name: 'iotManage',
        redirect: '/iotManage/doorManage',
        component: Main,
        children: [
            {
                path: 'doorManage',
                name: 'iotManage_doorManage',
                component: () => import('@/views/iotManage/doorManage.vue')
            },
            {
                path: 'doorDetail',
                name: 'iotManage_doorDetail',
                component: () => import('@/views/iotManage/doorDetail.vue')
            },
            {
                path: 'iot',
                name: 'iotManage_iot',
                component: () => import('@/views/iotManage/iot.vue')
            },
            {
                path: 'iotDetail',
                name: 'iotManage_iotDetail',
                component: () => import('@/views/iotManage/iotDetail.vue')
            },
        ]
    },
    {
        path: '/huanweiManage',
        name: 'huanweiManage',
        redirect: '/huanweiManage/huanwei',
        component: Main,
        children: [
            {
                path: 'huanwei',
                name: 'huanweiManage_huanwei',
                component: () => import('@/views/huanweiManage/huanwei.vue')
            },
        ]
    },
    {
        path: '/dianziweilan',
        name: 'dianziweilan',
        redirect: '/dianziweilan/dianziweilan',
        component: Main,
        children: [
            {
                path: 'dianziweilan',
                name: 'dianziweilan_dianziweilan',
                component: () => import('@/views/dianziweilan/dianziweilan.vue')
            },
        ]
    },
    {
        path: '/kaoqinArea',
        name: 'kaoqinArea',
        redirect: '/kaoqinArea/kaoqinArea',
        component: Main,
        children: [
            {
                path: 'kaoqinArea',
                name: 'kaoqinArea_kaoqinArea',
                component: () => import('@/views/kaoqinArea/kaoqinArea.vue')
            },
        ]
    },
    {
        path: '/qingjiaManage',
        name: 'qingjiaManage',
        redirect: '/qingjiaManage/qingjia',
        component: Main,
        children: [
            {
                path: 'qingjia',
                name: 'qingjiaManage_qingjia',
                component: () => import('@/views/qingjiaManage/qingjia.vue')
            },
        ]
    },
    {
        path: '/deviceManage',
        name: 'deviceManage',
        redirect: '/deviceManage/device',
        component: Main,
        children: [
            {
                path: 'device',
                name: 'deviceManage_device',
                component: () => import('@/views/deviceManage/device.vue')
            },
        ]
    },
    {
        path: '/yujingManage',
        name: 'yujingManage',
        redirect: '/yujingManage/sos',
        component: Main,
        children: [
            {
                path: 'sos',
                name: 'yujingManage_sos',
                component: () => import('@/views/yujingManage/sos.vue')
            },
            {
                path: 'weilan',
                name: 'yujingManage_weilan',
                component: () => import('@/views/yujingManage/weilan.vue')
            },
            {
                path: 'juji',
                name: 'yujingManage_juji',
                component: () => import('@/views/yujingManage/juji.vue')
            },
        ]
    },
    {
        path: '/kaoqinManage',
        name: 'kaoqinManage',
        redirect: '/kaoqinManage/kaoqinzu',
        component: Main,
        children: [
            {
                path: 'kaoqinzu',
                name: 'kaoqinManage_kaoqinzu',
                component: () => import('@/views/kaoqinManage/kaoqinzu.vue')
            },
            {
                path: 'addKaoqin',
                name: 'kaoqinManage_addKaoqin',
                component: () => import('@/views/kaoqinManage/addKaoqin.vue')
            },
            {
                path: 'paiban',
                name: 'kaoqinManage_paiban',
                component: () => import('@/views/kaoqinManage/paiban.vue')
            },
            {
                path: 'banci',
                name: 'kaoqinManage_banci',
                component: () => import('@/views/kaoqinManage/banci.vue')
            },
            {
                path: 'addBanci',
                name: 'kaoqinManage_addBanci',
                component: () => import('@/views/kaoqinManage/addBanci.vue')
            },
        ]
    },
    {
        path: '/system',
        name: 'system',
        redirect: '/system/account',
        component: Main,
        children: [
            {
                path: 'account',
                name: 'system_account',
                component: () => import('@/views/system/account.vue')
            },
            {
                path: 'company',
                name: 'system_company',
                component: () => import('@/views/system/company.vue')
            },
            {
                path: 'role',
                name: 'system_role',
                component: () => import('@/views/system/role.vue')
            },
        ]
    },
    {
        path: '**',
        redirect: '/qingjiaManage'
    },
]

const router = new VueRouter({
  routes
})


// 获取用户权限菜单
router.beforeEach((to, from, next) => {

    if (!/login|home/.test(to.name)) {
        if (!store.state.user.UserInfo || !store.state.user.UserInfo.id) {
            next({
                replace: true,
                name: "login"
            });
            return;
        }
    }

    next();
});

export default router
